<jsp:include page="logincheck.jsp" />
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>

<jsp:useBean id="bill" class="se2.Bill" scope="request"/>
<jsp:setProperty name="bill" property="*"/>

<jsp:useBean id="phone" class="se2.PhoneAccount" scope="request"/>
<jsp:setProperty name="phone" property="*"/>

<html>

	<head>
		<title>Administration Console</title>
		<link rel="stylesheet" href="default.css" type="text/css" media="screen"></link>
		<script src="sorttable.js"></script>
	</head>

	<body>
		<div id="container">
			<div id="navigation">
<p>&nbsp;</p>
<jsp:include page="navigation.jsp" />
			</div>
			
			<div id="header">				
				<img src="images/header.png" />
			</div>

			<div id="content">
<!-- Main code -->
				
				<!-- Initialize the Bill Object -->
				<% 
					boolean odd = false;
					DecimalFormat dcf = new DecimalFormat("#0.00");
					dcf.setDecimalSeparatorAlwaysShown(true);
					
					GregorianCalendar gc = new GregorianCalendar();
					GregorianCalendar gc2 = new GregorianCalendar();
					
					String start = request.getParameter("starttime");
					String end = request.getParameter("endtime");
					
					int syear = Integer.parseInt(start.substring(0,4));
					int smonth = Integer.parseInt(start.substring(5,7)) - 1;
					int sday = Integer.parseInt(start.substring(8,10));
					int shour = Integer.parseInt(start.substring(11,13));
					int smin = Integer.parseInt(start.substring(14,16));
					int ssec = Integer.parseInt(start.substring(17,19));
					
					int eyear = Integer.parseInt(end.substring(0,4));
					int emonth = Integer.parseInt(end.substring(5,7)) - 1;
					int eday = Integer.parseInt(end.substring(8,10));
					int ehour = Integer.parseInt(end.substring(11,13));
					int emin = Integer.parseInt(end.substring(14,16));
					int esec = Integer.parseInt(end.substring(17,19));
					
					gc.set(syear, smonth, sday, shour, smin, ssec);
					gc2.set(eyear, emonth, eday, ehour, emin, esec);
					bill.setStartDate(gc);
					bill.setEndDate(gc2);
					bill.setPhoneAccount(Integer.parseInt(request.getParameter("id")));
					bill.getBill();
					
					bill.calculateTotal();
					
					phone.setId(Integer.parseInt(request.getParameter("id")));
					phone.getPhoneAccountFromDatabase();
				%>
								
				<h1>Phone Bill :: View</h1>
				
				<form name="chargeBill" id="chargeBill" method="post" action="chargeBillFunction.jsp?id=<%= request.getParameter("id") %>&cid=<%= request.getParameter("id") %>">
					
				<input type="hidden" name="generatestart" id="generatestart" value="<%=request.getParameter("starttime")%>"/>
				<input type="hidden" name="generateend" id="generateend" value="<%=request.getParameter("endtime")%>"/>
					
					<table class="sortable" width="100%" border="0" cellpadding="0" cellspacing="0">
									
						<tr style="background-image: url('images/tableBackground.png');">
							<th style="background-image: url('images/tableBackground.png');" width="*" class="sorttable_nosort tableHeader">&nbsp;</th>
							<th style="background-image: url('images/tableBackground.png');" width="*" class="tableHeader" align="left">Time</th>
							<th style="background-image: url('images/tableBackground.png');" width="*" class="tableHeader" align="center">Extension Dialed</th>
							<th style="background-image: url('images/tableBackground.png');" width="*" class="tableHeader" align="center">Duration (mins)</th>
							<th style="background-image: url('images/tableBackground.png');" width="*" class="tableHeader" align="center">Amount</th>
							<th style="background-image: url('images/tableBackground.png');" width="*" class="sorttable_nosort tableHeader">&nbsp;</th>
						</tr>
	
						<c:forEach var="callRecord" items="${bill.callRecords}">
							<% if (odd) { %>
								<tr style="background-image: url('images/tableBackground.png');">
							<% } else { %>
								<tr>
							<% } odd = !odd; %>
								<td>&nbsp;</td>
								<td sorttable_customkey="${callRecord.sortStartTime}">${callRecord.formattedStartTime}</td>
								<td align="center">${callRecord.toExtension}</td>
								<td align="center">${callRecord.duration}</td>
								<td align="right">${callRecord.formattedAmount}</td>
								<td>&nbsp;</td>
							</tr>
						</c:forEach>
						<tfoot>
						<tr><td>Bill Total</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td align="right">$<%= dcf.format(bill.getTotal())%></td></tr>
						<tr><td>Existing Balance</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td align="right">$<%= dcf.format(phone.getBalance())%></td></tr>
						<tr><td>Final Total</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td align="right">$<%= dcf.format(phone.getBalance() + bill.getTotal()) %></td></tr>
						<tr>
						<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
							<td colspan="2" align="right">
								<input type="submit" value="Submit" />
								<input type="button" value="Cancel" onclick="javascript:document.location.href='viewBill.jsp?id=<%= request.getParameter("id") %>&cid=<%= request.getParameter("cid") %>'" />
							</td>
						</tr>
						</tfoot>
					</table>
				</form>

<!-- Main Code -->
			</div>

			<div id="footer">
<jsp:include page="footer.jsp" />
			</div>
		</div>
	</body>

</html>